var EconomicAbilityInfoDlg = {
    economicAbilityInfoData : {},
    zTreeInstance : null,
    validateFields: {
        economicAbility: {
            validators: {
                notEmpty: {
                    message: '经济能力不能为空'
                },
                stringLength: {
                    max: 20,
                    message: '经济能力长度不能超过20，请重新输入...'
                }/*最后一个没有逗号*/
            }
        },
    }
};

/**
 * 清除数据
 */
EconomicAbilityInfoDlg.clearData = function() {
    this.economicAbilityInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
EconomicAbilityInfoDlg.set = function(key, val) {
    this.economicAbilityInfoData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
EconomicAbilityInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
EconomicAbilityInfoDlg.close = function() {
    parent.layer.close(window.parent.EconomicAbility.layerIndex);
}

/**
 * 点击部门ztree列表的选项时
 *
 * @param e
 * @param treeId
 * @param treeNode
 * @returns
 */
EconomicAbilityInfoDlg.onClickEconomicAbility = function(e, treeId, treeNode) {
    $("#pName").attr("value", EconomicAbilityInfoDlg.zTreeInstance.getSelectedVal());
    $("#pid").attr("value", treeNode.id);
}

/**
 * 显示部门选择的树
 *
 * @returns
 */
EconomicAbilityInfoDlg.showEconomicAbilitySelectTree = function() {
    var pName = $("#pName");
    var pNameOffset = $("#pName").offset();
    $("#parentEconomicAbilityMenu").css({
        left : pNameOffset.left + "px",
        top : pNameOffset.top + pName.outerHeight() + "px"
    }).slideDown("fast");

    $("body").bind("mousedown", onBodyDown);
}

/**
 * 隐藏部门选择的树
 */
EconomicAbilityInfoDlg.hideEconomicAbilitySelectTree = function() {
    $("#parentEconomicAbilityMenu").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);// mousedown当鼠标按下就可以触发，不用弹起
}

/**
 * 收集数据
 */
EconomicAbilityInfoDlg.collectData = function() {
    this.set('id').set('economicAbility').set('num').set('remarks');
}

/**
 * 验证数据是否为空
 */
EconomicAbilityInfoDlg.validate = function () {
    $('#economicAbilityInfoForm').data("bootstrapValidator").resetForm();
    $('#economicAbilityInfoForm').bootstrapValidator('validate');
    return $("#economicAbilityInfoForm").data('bootstrapValidator').isValid();
}

/**
 * 提交添加
 */
EconomicAbilityInfoDlg.addSubmit = function() {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    var ajax = new $ax(Feng.ctxPath + "/economicAbility/add", function(data){
        Feng.success("添加成功!");
        window.parent.EconomicAbility.table.refresh();
        EconomicAbilityInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.economicAbilityInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
EconomicAbilityInfoDlg.editSubmit = function() {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    var ajax = new $ax(Feng.ctxPath + "/economicAbility/update", function(data){
        Feng.success("修改成功!");
        window.parent.EconomicAbility.table.refresh();
        EconomicAbilityInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.economicAbilityInfoData);
    ajax.start();
}

function onBodyDown(event) {
    if (!(event.target.id == "menuBtn" || event.target.id == "parentEconomicAbilityMenu" || $(
            event.target).parents("#parentEconomicAbilityMenu").length > 0)) {
        EconomicAbilityInfoDlg.hideEconomicAbilitySelectTree();
    }
}

$(function() {
    Feng.initValidator("economicAbilityInfoForm", EconomicAbilityInfoDlg.validateFields);
});
